package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.FragmentStareAdapter;
import com.tianxin.workbao.workBao.bean.CarInformation;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.fragment.CarMoneyStatisticFragment;

/**
 * Created by hacker on 2017/7/26.
 */

public class CarMoneyStatisticActivity extends BaseActivity implements View.OnClickListener {
    private TextView toolbarTitle;
    private ViewPager mViewPager;
    private FragmentStareAdapter mAdapter;
    private TabLayout mTabLayout;
    private FloatingActionButton top_fab;
    private CarInformation carInformation;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_main_tablayout);
        super.initStatusBar(true);

        initView();
        initData();
        initEvent();
        bindView();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("");
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));
        setSupportActionBar(mToolbar);
        toolbarTitle = (TextView) findViewById(R.id.toolbar_title);
        toolbarTitle.setText(getResources().getString(R.string.car_money));
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setCurrentItem(1);

        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
        mTabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.write));
        top_fab = (FloatingActionButton) findViewById(R.id.top_fab);
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            carInformation = (CarInformation) bundle.getSerializable("car");
        }

        mTabTitles.add(0, "入账");
        mTabTitles.add(1, "出账");

        String plateNumber;
        if (carInformation != null) {
            plateNumber = carInformation.getPlatenumbers();
        } else {
            plateNumber = null;
        }
        mFragments.add(0, CarMoneyStatisticFragment.newInstance(Constant.CAR_FINANCE_IN));
        mFragments.add(1, CarMoneyStatisticFragment.newInstance(Constant.CAR_FINANCE_OUt));

        mAdapter = new FragmentStareAdapter(getSupportFragmentManager(), mFragments, mTabTitles);
    }

    @Override
    public void initEvent() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CarMoneyStatisticActivity.this.finish();
            }
        });
        top_fab.setOnClickListener(this);
    }

    @Override
    public void bindView() {
        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setAdapter(mAdapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_empty, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        switch (id) {
            case R.id.action_add:
                Toast.makeText(this, R.string.developing, Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_Share:
                Toast.makeText(this, R.string.developing, Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onRestart() {
        updateUi();
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void updateUi() {
        mFragments.get(mTabLayout.getSelectedTabPosition()).initData(false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_fab:
                updateUi();
                break;
        }
    }
}
